package spotGUI.GUIElements.MarkerGUI;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class VoteDisplay extends JPanel {
	
	private JLabel[] labels = new JLabel[5];

	private ImageIcon fullStar = new ImageIcon(VoteDisplay.class.getResource("/images/Icons/32x32/favorite.png"));
	private ImageIcon emptyStar = new ImageIcon(VoteDisplay.class.getResource("/images/Icons/32x32/empty_favorite.png"));
	private ImageIcon halfFullStar = new ImageIcon(VoteDisplay.class.getResource("/images/Icons/32x32/half_favorite.png"));

	
	public VoteDisplay(float vote) {
			if (vote > 5 || vote < 0) {
				throw new SpotMessengerVoteException();
		}
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		for (int i = 0; i < labels.length; i++) {
			labels[i] = new JLabel(emptyStar);
			add(labels[i]);
		}
	
		fillUntil(roundVote(vote), containsHalfVote(vote));
		
	}
	
	private void fillUntil (int vote, boolean halfVote) {
		for (int i = 0; i < vote; i++) {
			labels[i].setIcon(fullStar);
		}
		if (halfVote == true && vote!=5) {
			labels[vote].setIcon(halfFullStar);
		}
	}
	
	private int roundVote (float vote) {
		return Math.round(vote);
	}
	
	private boolean containsHalfVote (float vote) {
		float temp = vote-(int)vote;
		if (temp > 0 && temp < 0.5f) {
			return true;
		}
		return false;
	}

}

class SpotMessengerVoteException extends NumberFormatException {
	public SpotMessengerVoteException() {
		super("Vote value is out of range.");
	}
}

